package java6681.lesson14;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @author Junichiro
 * @description
 * @date 2021-06-18 15:19
 **/
//java java6681.lesson14.BaseClient6681
//javac -encoding UTF-8 java6681/lesson14/BaseClient6681.java
public class BaseClient6681 {
    public static void main(String[] args) throws IOException {
        //（1）按给定的IP和端口号向服务器端发送连接请求
        //（2）建立连接后，得到与该服务器端通信的Socket对象
        Socket s = new Socket(InetAddress.getLocalHost(), 8888);
        //（3）发送信息：通过Socket对象的输出流向服务器端发送信息
        OutputStream os = s.getOutputStream();
        System.out.println("客户拨号...");
        os.write("接听电话...空调不制冷--6681".getBytes());

        //（4）接收信息：从Socket对象的输入流中接收服务器端传来的信息，显示在控制台上
        InputStream is = s.getInputStream();
        byte[] bys = new byte[1024];
        int len = is.read(bys);
        String client = new String(bys, 0, len);
        System.out.println(client);
        //（5）关闭
        s.close();
    }
}
